package controller;

import java.util.HashMap;
import java.util.Map;

import entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.BookService;
import util.PageBean;


@Controller
public class BookController {
	
	@Autowired
	private BookService bookService;
	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}


	@RequestMapping("/list")
	public String list(@RequestParam(defaultValue = "3") Integer pageSize,
			@RequestParam(defaultValue = "1") Integer pageIndex,ModelMap mm){
		Map<String, Object> condition=new HashMap<String, Object>();
		condition.put("pageSize", pageSize);
		condition.put("pageIndex", pageIndex);
		PageBean<Book> pageBean = bookService.findPage(condition);
		mm.addAttribute("pageBean",pageBean);
		return "forward:index.jsp";
	}
	
	@RequestMapping("/add")
	public String add(Book book,String names){
		book.setName(names);
		int count = bookService.save(book);
		return "redirect:/list";
	}
		
	@RequestMapping("/toUpdate")
	public String toUpdate(Integer id,ModelMap mm){
		Book book = bookService.findById(id);
		mm.addAttribute("book",book);
		return "forward:/update.jsp";
	}
	
	@RequestMapping("/update")
	public String update(Book book){
		int count = bookService.update(book);
		return "redirect:/list";
	}
	
	@RequestMapping("remove")
	public String remove(Integer id){
		int count = bookService.remove(id);
		return "redirect:/list";
	}
	
}
